﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Algobox.JuicyLib.SQL.LiquidView;
using Algobox.JuicyLib.Apps.Yahoo;

namespace LiquidTrading.LiquidViewSyncTool
{
    class Yahoo
    {

        public static void ProcessYahoo()
        {
            DateTime date;
            int MAXASYNC = 40;
            int countAsync = 0;
            bool started;

            if (DateTime.Today.DayOfWeek == DayOfWeek.Sunday)
                date = DateTime.Today.Subtract(new TimeSpan(2, 0, 0, 0));
            if (DateTime.Today.DayOfWeek == DayOfWeek.Monday)
                date = DateTime.Today.Subtract(new TimeSpan(3, 0, 0, 0));
            else
                date = DateTime.Today.Subtract(new TimeSpan(1, 0, 0, 0));


            var stocks = Program.DBLinq.InstrumentsStocks.Where(a => a.MarketMIC == "XLON" && a.Reuters != null && a.UMTF != null);

            foreach (InstrumentsStock stock in stocks)
            {
                if (stock.Reuters != String.Empty)
                {
                    started = YahooClient.TryGetPreviousClose(stock.Reuters, stock, (result, price, handle) =>
                    {
                        int retval;
                        InstrumentsStock innerStock = (InstrumentsStock)handle;
                        if (result)
                        {
                            if (innerStock.CurrencyId == 1280)
                                price = price / 100;

                            LiquidViewHelper.TryImportPriceByUMTF(date, innerStock.UMTF, "XLON", null, null, null, price, price, null, "LiquidViewSyncTool - Yahoo! Finance", out retval);

                            if (retval == 0)
                                Console.WriteLine("Success " + innerStock.UMTF + " " + price);
                            else
                                Console.WriteLine("Failed to import {0} {1} with return value of {2}", innerStock.UMTF, price, retval);
                        }
                        else
                        {
#if DEBUG
                            Console.WriteLine("Failed to get Yahoo price for {0}", innerStock.Reuters);
#endif
                            // try with a different symbol
                            if (!String.IsNullOrEmpty(stock.YahooFinance))
                            {
                                YahooClient.TryGetPreviousClose(stock.YahooFinance, stock, (innerResult, innerPrice, innerHandle) =>
                                {
                                    if (innerResult)
                                    {
                                        if (innerStock.CurrencyId == 1280)
                                            price = price / 100;

                                        LiquidViewHelper.TryImportPriceByUMTF(date, innerStock.UMTF, "XLON", null, null, null, price, price, null, "LiquidViewSyncTool - Yahoo! Finance", out retval);

                                        if (retval == 0)
                                            Console.WriteLine("Success " + innerStock.UMTF + " " + innerPrice.ToString());
                                        else
                                            Console.WriteLine("Failed to import {0} {1} with return value of {2}", innerStock.UMTF, innerPrice.ToString(), retval);
                                    }
                                    else
                                    {
#if DEBUG
                                        Console.WriteLine("Failed to get Yahoo price for {0}", innerStock.YahooFinance);
#endif
                                    }

                                });
                            }
                        }
                        --countAsync;
                    });

                    if (started)
                        ++countAsync;

                    System.Threading.Thread.Sleep(100);
                    started = false;

                    while (countAsync >= MAXASYNC)
                        System.Threading.Thread.Sleep(10000);

                }
            }

        }

    }
}
